package org.tuoren.official.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.official.domain.vo.TProductCarouselVo;
import org.tuoren.official.domain.bo.TProductCarouselBo;
import org.tuoren.official.service.ITProductCarouselService;
import org.tuoren.common.mybatis.core.page.TableDataInfo;

/**
 * 产品中心轮播图
 *
 * @author Lion Li
 * @date 2025-03-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/productCarousel")
public class TProductCarouselController extends BaseController {

    private final ITProductCarouselService tProductCarouselService;

    /**
     * 前台-查询产品中心轮播图列表
     */
    @SaIgnore
    @GetMapping("/frontList")
    public TableDataInfo<TProductCarouselVo> frontList() {
        return tProductCarouselService.queryFrontList();
    }
    /**
     * 查询产品中心轮播图列表
     */
    @SaCheckPermission("official:productCarousel:list")
    @GetMapping("/list")
    public TableDataInfo<TProductCarouselVo> list(TProductCarouselBo bo, PageQuery pageQuery) {
        return tProductCarouselService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品中心轮播图列表
     */
    @SaCheckPermission("official:productCarousel:export")
    @Log(title = "产品中心轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TProductCarouselBo bo, HttpServletResponse response) {
        List<TProductCarouselVo> list = tProductCarouselService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品中心轮播图", TProductCarouselVo.class, response);
    }

    /**
     * 获取产品中心轮播图详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:productCarousel:query")
    @GetMapping("/{id}")
    public R<TProductCarouselVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tProductCarouselService.queryById(id));
    }

    /**
     * 新增产品中心轮播图
     */
    @SaCheckPermission("official:productCarousel:add")
    @Log(title = "产品中心轮播图", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TProductCarouselBo bo) {
        return toAjax(tProductCarouselService.insertByBo(bo));
    }

    /**
     * 修改产品中心轮播图
     */
    @SaCheckPermission("official:productCarousel:edit")
    @Log(title = "产品中心轮播图", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TProductCarouselBo bo) {
        return toAjax(tProductCarouselService.updateByBo(bo));
    }

    /**
     * 删除产品中心轮播图
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:productCarousel:remove")
    @Log(title = "产品中心轮播图", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tProductCarouselService.deleteWithValidByIds(List.of(ids), true));
    }
}
